<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.dataaccess.modules.${moduleName}.dao.${ClassName}Dao">
    
	<sql id="${sqlId}">
	<#list columns as cln>
		<#if cln_index != 0>,</#if><#if cln.javaName == 'createBy'>a.create_by AS "createBy.id"<#elseif cln.javaName == 'updateBy'>a.update_by AS "updateBy.id"<#else>a.${cln.name} AS "${cln.javaName}"</#if>
	</#list>
	</sql>
    
	<select id="get" resultType="${ClassName}">
		select
			<include refid="${sqlId}"/>
		from ${tableName} a
		where a.${tableId} = ${r"#{"}${entityId}${r"}"}
	</select>
	
	<select id="findList" resultType="${ClassName}">
		select
			<include refid="${sqlId}"/>
		from ${tableName} a
		<where>
			<!-- ${r"${sqlMap.dsf}"}-->
			a.del_flag = ${r"#{DEL_FLAG_NORMAL}"}
			<#list columns as cln>
			<if test="${cln.javaName} != null <#if cln.jdbcTypeName=='VARCHAR'>and ${cln.javaName}!=''</#if>">
				and a.${cln.name}=${r"#{"}${cln.javaName},jdbcType=${cln.jdbcTypeName}${r"}"}
			</if>
			</#list>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				order by ${r"${page.orderBy}"}
			</when>
		</choose>
	</select>
	
	<select id="findAllList" resultType="${ClassName}">
		select
			<include refid="${sqlId}"/>
		from ${tableName} a
		<where>
            a.del_flag = ${r"#{DEL_FLAG_NORMAL}"}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				order by ${r"${page.orderBy}"}
			</when>
		</choose>
	</select>
	
	<insert id="insert">
		insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
		<#list columns as cln>
			<#if cln.javaName == 'createBy'>
			<if test="createBy != null and createBy.id!=null and createBy.id!=''">
				${cln.name},
            </if>
			<#elseif cln.javaName  == 'updateBy'>
            <if test="updateBy != null and updateBy.id!=null and updateBy.id!=''">
				${cln.name},
            </if>
			<#else>
			<if test="${cln.javaName} != null <#if cln.jdbcTypeName=='VARCHAR'>and ${cln.javaName}!=''</#if>">
				${cln.name},
			</if>
			</#if>
		</#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
		<#list columns as cln>
			<#if cln.javaName == 'createBy'>
            <if test="createBy != null and createBy.id!=null and createBy.id!=''">
				${r"#{createBy.id,jdbcType=VARCHAR}"},
            </if>
			<#elseif cln.javaName  == 'updateBy'>
            <if test="updateBy != null and updateBy.id!=null and updateBy.id!=''">
                ${r"#{updateBy.id,jdbcType=VARCHAR}"},
            </if>
			<#else>
			<if test="${cln.javaName} != null <#if cln.jdbcTypeName=='VARCHAR'>and ${cln.javaName}!=''</#if>">
				${r"#{"}${cln.javaName},jdbcType=${cln.jdbcTypeName}${r"}"},
            </if>
			</#if>
		</#list>
        </trim>
	</insert>
	
	<update id="update">
		update ${tableName}
        <set>
		<#list columns as cln>
		<#if cln.javaName == 'createBy'>
            <if test="createBy != null and createBy.id!=null and createBy.id!=''">
                create_by=${r"#{createBy.id,jdbcType=VARCHAR}"},
            </if>
		<#elseif cln.javaName  == 'updateBy'>
            <if test="updateBy != null and updateBy.id!=null and updateBy.id!=''">
                update_by=${r"#{updateBy.id,jdbcType=VARCHAR}"},
            </if>
		<#else>
			${cln.name} = ${r"#{"}${cln.javaName},jdbcType=${cln.jdbcTypeName}${r"}"},
		</#if>
		</#list>
        </set>
		where ${tableId} = ${r"#{"}${entityId}${r"}"}
	</update>

	<update id="updateSelective">
		update ${tableName}
		<set>
		<#list columns as cln>
        <#if cln.javaName == 'createBy'>
            <if test="createBy != null and createBy.id!=null and createBy.id!=''">
               create_by=${r"#{createBy.id,jdbcType=VARCHAR}"},
            </if>
        <#elseif cln.javaName  == 'updateBy'>
            <if test="updateBy != null and updateBy.id!=null and updateBy.id!=''">
                update_by=${r"#{updateBy.id,jdbcType=VARCHAR}"},
            </if>
        <#else>
            <if test="${cln.javaName} != null <#if cln.jdbcTypeName=='VARCHAR'>and ${cln.javaName}!=''</#if>">
              ${cln.name}=${r"#{"}${cln.javaName},jdbcType=${cln.jdbcTypeName}${r"}"},
            </if>
        </#if>
		</#list>
		</set>
		where ${tableId} = ${r"#{"}${entityId}${r"}"}
	</update>

	<update id="delete">
		update ${tableName} SET
        	update_time = now(),
			del_flag = ${r"#{DEL_FLAG_DELETE}"}
		where ${tableId} =${r"#{"}${entityId}${r"}"}
	</update>
</mapper>